package com.btex.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.btex.app.Config;
import com.btex.app.R;
import com.btex.app.entity.CoinInfo;
import com.btex.app.tools.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/20.
 */
public class CoinsAdapter extends BaseAdapter {
    private Context cxt;
    private List<CoinInfo> data = new ArrayList<>();
    private String TAG = "CoinsAdapter";

    public CoinsAdapter(Context cxt) {
        this.cxt = cxt;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    public void addData(boolean clear, List<CoinInfo> coinInfos) {
        if (clear) {
            data.clear();
            LogUtil.i(TAG, "刷新数据");
        }
        data.addAll(coinInfos);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return (data == null || data.size() == 0) ? null : (data.size() >= position ? data.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(cxt).inflate(
                    R.layout.item_coin_trade, null);
            holder = new ViewHolder();
            holder.tvCoinName = (TextView) convertView.findViewById(R.id.tvCoinName);
            holder.tvCoinPrice = (TextView) convertView.findViewById(R.id.tvCoinPrice);
            holder.tvMaxPrice = (TextView) convertView.findViewById(R.id.tvMaxPrice);
            holder.tvMinPrice = (TextView) convertView.findViewById(R.id.tvMinPrice);
            holder.tvZhangDieFu = (TextView) convertView.findViewById(R.id.tvZhangDieFu);
            holder.tvZhangDieFu = (TextView) convertView.findViewById(R.id.tvZhangDieFu);
            holder.tvTradeNum = (TextView) convertView.findViewById(R.id.tvTradeNum);
            holder.ivCoinImg = (ImageView) convertView.findViewById(R.id.ivCoinImg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CoinInfo coinInfo = (CoinInfo) getItem(position);
        if (coinInfo != null) {
            holder.tvCoinName.setText(coinInfo.getZh_cn());
            holder.tvCoinPrice.setText("¥" + coinInfo.getTrade_price());
            holder.tvMaxPrice.setText(coinInfo.getHigh());
            holder.tvMinPrice.setText(coinInfo.getLow());
            holder.tvTradeNum.setText(coinInfo.getAmount());
            //          String zhangDieFu = Tools.decimalFormatTool(Integer.parseInt(coinInfo.getFloat_num())).format(Double.parseDouble(coinInfo.getChange_24h())*100);

            double d = Double.parseDouble(coinInfo.getChange_24h());
            String result = String.format("%.2f", d * 100) + "%";

            new BitmapUtils(cxt).display(holder.ivCoinImg, Config.URL + coinInfo.getImg());
            if (d < 0) {
                holder.tvZhangDieFu.setText(result);
                holder.tvZhangDieFu.setTextColor(cxt.getResources().getColor(R.color.tv_green));
            } else if (d > 0) {
                holder.tvZhangDieFu.setText("+" + result);
                holder.tvZhangDieFu.setTextColor(cxt.getResources().getColor(R.color.tv_red));
            } else {
                holder.tvZhangDieFu.setText(result);
                holder.tvZhangDieFu.setTextColor(cxt.getResources().getColor(R.color.tv_black));
            }
        }

        return convertView;
    }

    private static class ViewHolder {
        private ImageView ivCoinImg;
        private TextView tvCoinName;
        private TextView tvCoinPrice;
        private TextView tvMaxPrice;
        private TextView tvMinPrice;
        private TextView tvZhangDieFu;
        private TextView tvTradeNum;
    }
}
